package com.baitengsoft.patentapimonitor.api.controller;

import cn.hutool.core.date.DateUtil;
import com.baitengsoft.patentapimonitor.api.entity.FileMonitorReportEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.param.SearchMonSummaryParam;
import com.baitengsoft.patentapimonitor.api.service.FileMonitorReportService;
import com.baitengsoft.patentapimonitor.common.utils.DateUtils;
import com.baitengsoft.patentapimonitor.common.utils.ExcelUtil;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author Chen 2018/2/5
 */
@RestController
@RequestMapping("/api/v1/web/report")
@Api(value = "前台-统计报表-监控报表",description = "监控报表")
public class WebReportController extends AbstractController {

    @Autowired
    private FileMonitorReportService fileMonitorReportService;

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value ="列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "title", value = "标题", required = false),
    })
    public Res getApplyperson(String title){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        List<FileMonitorReportEntity> report =fileMonitorReportService.queryList(getUserInfo().getPlatformid(),title);
        return Res.ok().put("list",report);
    }

    @GetMapping("/generate")
    @ResponseBody
    @ApiOperation(value ="生成")
    public Res generateReport(){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        //当前日期字符串，格式：yyyy-MM-dd
        FileMonitorReportEntity entity = fileMonitorReportService.selectByDate(userInfo.getPlatformid(),DateUtil.today());
//        if (entity !=null){
//            return Res.error("今日已生成过");
//        }
        fileMonitorReportService.createFile(userInfo.getPlatformid());
        return Res.ok();
    }

    @ApiOperation(value ="下载")
    @GetMapping("/download")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "fileurl", value = "fileurl", required = true)
    })
    public Res download(@ApiIgnore String fileurl , HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        FileMonitorReportEntity entity = fileMonitorReportService.selectByFileurl(userInfo.getPlatformid(),fileurl);
        if (entity == null){
            return Res.error("未查询到该报表");
        }
        return fileMonitorReportService.exportReportFile(userInfo.getPlatformid(),entity,response);
    }
}
